package com.banan.service.impl;

import com.banan.EnumConstant.TypeEnum;
import com.banan.dao.mapper.SercetTableMapper;
import com.banan.entity.SercetTable;
import com.banan.service.ISercetService;
import com.banan.util.Constant;
import com.banan.util.SercetHandle;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 类描述
 * <p>
 * Created by lanceq
 * 2016/8/5.
 **/
@Log4j
@Service
public class SercetServiceImpl implements ISercetService {
    @Autowired
    private SercetTableMapper SercetTableMapper;

    @Override
    public boolean createSercet(List<SercetTable>  listmap) {
        log.info(">>>>正在创建密码*************");
        long current=System.currentTimeMillis();
        List<SercetTable> saveList=new ArrayList<SercetTable>();
        for (SercetTable table : listmap) {
            SercetTable sercet=new SercetTable();
                try {
                    table.setPassword(SercetHandle.aesEncryptToBytes(table.getPasswordText(),table.getSalt()));
                    sercet.setAccount(table.getAccount());
                    sercet.setName(table.getName());
                    sercet.setSalt(table.getSalt());
                    sercet.setPassword(table.getPassword());
                    sercet.setType(TypeEnum.getNamebyCode(table.getType()));//此处type为 枚举中 typeCode
                    saveList.add(sercet);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(">>>>.加密过程失败,e:",e);
                }

        }
        //保存数据库 以及序列化到 文档

        int result=SercetTableMapper.insertBaich(saveList);
       log.info(">>>批量插入条数："+result);
        if(result<1){
            return false;
        }
        StringBuffer sbf=new StringBuffer();
        sbf.append("******* this  is  head ************\r\n");
        sbf.append("[Type]\t\t[Name]\t\t[ACCOUNT]\t\t[PASSWORD]\t\t[SALT] \r\n");
        for (SercetTable sercetTable : saveList) {
            sbf.append(sercetTable.getType() + "\t\t" + sercetTable.getName() +
                    "\t\t" + sercetTable.getAccount() + "\t\t" + sercetTable.getPassword() + "\t\t"
                    + sercetTable.getSalt() + ",\r\n");
        }
        File file=new File(Constant.FILE_PATH);
        try {
            if(file.exists()){
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(String.valueOf(sbf));
            bw.close();
        } catch (FileNotFoundException e) {
            log.error(">>>>创建文件错误.................");
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        long currentEnd=System.currentTimeMillis();
        log.info(">>创建完成...........耗时:"+(currentEnd-current));
        return true;
    }


    @Override
    public boolean DecryptSercet(List<HashMap<String, String>>  map) {
        return false;
    }
}
